package com.rongxun.hbwallet.ui.activities;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongxun.base.HomeFragVpAdapter;
import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.ErrorCode;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.mvp.contract.ILoadWallet;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.LoadWalletPresenter;
import com.rongxun.hbwallet.ui.fragment.loadwallet.LoadWalletByMnemonicFragment;
import com.rongxun.hbwallet.ui.fragment.loadwallet.LoadWalletByOfficialWalletFragment;
import com.rongxun.hbwallet.ui.fragment.loadwallet.LoadWalletByPrivateKeyFragment;
import com.rongxun.utils.FlyLog;
import com.rongxun.utils.PreferenceUtils;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.view.ViewPagerIndicator;
import com.rongxun.widget.LoadingDialog;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/22/022.
 * e-mail:lomapa@163.com
 */

public class LoadWalletActivity extends MvpActivity<ILoadWallet.IView, LoadWalletPresenter> implements ILoadWallet.IView {
    @BindView(R.id.id_indicator)
    ViewPagerIndicator indicatorView;
    @BindView(R.id.vp_load_wallet)
    ViewPager          vpLoadWallet;
    @BindView(R.id.toolbar_title)
    TextView           mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar            mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView          mToolbarBack;
    private Dialog loadingDialog;

    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.load_wallet_title));
        loadingDialog = LoadingDialog.createLoadingDialog(this, getString(R.string.load_wallet_ing), false);
        String[] titles = {getString(R.string.load_wallet_indicator_mnemonic), getString(R.string.load_wallet_indicator_official)
                , getString(R.string.load_wallet_indicator_private_key)};


        final String[] fnames = {
                LoadWalletByMnemonicFragment.class.getName(),
                LoadWalletByOfficialWalletFragment.class.getName(),
                LoadWalletByPrivateKeyFragment.class.getName(),
        };
        HomeFragVpAdapter adapter = new HomeFragVpAdapter(getSupportFragmentManager(), this, fnames);

        indicatorView.setVisibleTabCount(3);
        indicatorView.setTabItemTitles(Arrays.asList(titles));

        vpLoadWallet.setAdapter(adapter);
        indicatorView.setViewPager(vpLoadWallet, 0);
        getPresenter().checkPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    @Override
    protected int getContentView() {

        return R.layout.activity_load_wallet;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {
        if (show)
            loadingDialog.show();
        else
            loadingDialog.cancel();
    }

    @Override
    public void showError(String msg, String errorCode) {
        switch (errorCode) {
            case ErrorCode.NO_PERMISSION:
                ToastUtils.showToast(R.string.no_permission);
                finish();
                break;
        }
    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }


    @Override
    public void release() {
        if (loadingDialog != null)
            loadingDialog.cancel();
    }

    @NonNull
    @Override
    public LoadWalletPresenter createPresenter() {
        return new LoadWalletPresenter(this, new ApiModel(this));
    }

    @Override
    public void loadSuccess(ETHWallet wallet) {
        getPresenter().saveWallet(PreferenceUtils.getString(SPKey.USERID, "0"), wallet);
    }


    @Override
    public void showSaveWalletResult(ETHWallet wallet) {
        FlyLog.i("ethWallet" + wallet.toString());

        ToastUtils.showToast(R.string.load_wallet_success);
        finish();
    }

    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        onBackPressed();
    }
}
